package cn.edu.nwpu.manager.controller;

import cn.edu.nwpu.manager.bean.Account;
import cn.edu.nwpu.manager.service.LoginService;

import cn.edu.nwpu.manager.utils.Response;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Hutengzhong
 * @version 1.0.0
 * @title LoginController
 * @description 登录管理，不要求大家写。如果之后需要用到，再单独去做安全验证
 * @date 2023/9/15
 */
@RestController
@RequestMapping("/Login")
public class LoginController {
    @Autowired
    SecurityManager securityManager;

    @Autowired
    LoginService loginService;

    /**
     * 检查账号是否存在
     * @param map: Request{url='Login/checkAccount', methods='POST', parameters={userAccount=admin}}
     * @return Response(200, "成功", true)
     */
    @PostMapping("checkAccount")
    public Response checkAccount(@RequestBody Map<String, Object> map)  {
        System.out.println("checkAccount");
        Response response;
        System.out.println("--------------ssssss---------------");
        Account account = new Account((HashMap) map);
        Integer userId = null;
        userId = loginService.checkAccount(account.getUserAccount());
        if (userId != null){
            // 存在
            response = new Response(200, "账号存在", true);
            response.setData(userId);
        }else {
            // 不存在
            response = new Response(0, "账号不存在", true);
        }
        return  response;
    }

    /**
     * 密码检查是否正确
     * @param map: Request{url='Login/checkPassword', methods='POST', parameters={userPassWord=123456, userAccount=admin}}
     * @return: Response
     */
    @PostMapping("checkPassword")
    public Response checkPassword(@RequestBody Map<String, Object> map){
        System.out.println("checkPassword");
        Response response;
        Account account = new Account((HashMap) map);
        Integer userId = null;
        account = loginService.chackPassword(account);
        userId = account.getUserId();
        if (userId != null){
            // 存在
            response = new Response(200, "账号存在", true);
            response.setData(userId);
        }else {
            // 不存在
            response = new Response(0, "账号不存在", true);
        }
        return  response;
    }

    /**
     * 根据账号密码登录
     * @param map: Request{url='Login/login', methods='POST', parameters={userPassWord=123456, userAccount=admin}}
     * @return: Response(200, "成功", true)
     */
    @PostMapping("login")
    public Response login(@RequestBody Map<String, Object> map){
        System.out.println("login");

        String userAccount = (String) map.get("userAccount");
        String userPassWord = (String) map.get("userPassWord");

        //做shrio安全认证
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userAccount, userPassWord);

        Response response;
        Account account = new Account((HashMap) map);
        try {
            subject.login(token);
            response = new Response(200, "账号存在", true);
            account = loginService.chackPassword(account);
            System.out.println(account);
            response.setData(account);
        }catch (Exception e){
            e.printStackTrace();
            response = new Response(0, "账号不存在", true);
        }
        /*account = loginService.chackPassword(account);
        userId = account.getUserId();
        if (userId != null){
            // 存在
            response = new Response(200, "账号存在", true);
            response.setData(account);
        }else {
            // 不存在
            response = new Response(0, "账号不存在", true);
        }*/
        return  response;
    }

}
